angular.module('mychat2').controller('RoomsCtrl',['$scope','$location','server',function($scope,$location,server){
  	$scope.searchRoom = function(){
		console.log('searchRoom');
		if($scope.searchKey){
			$scope.rooms = $scope._rooms.filter(function(room){
				return room.roomName.indexOf($scope.searchKey)>-1;
			});
		}else{
			$scope.rooms = $scope._rooms;
		}
	}
	$scope.createRoom  = function(){
		server.createRoom($scope.searchKey);
	}
	$scope.enterRoom = function(room){
		$location.path('/rooms/' + room._id);
	}
	$scope.rooms = $scope._rooms = server.getAllRooms();
	$scope.$watchCollection('_rooms', function() {
    	$scope.searchRoom();
  	});
}]);